package com.magictower.primary.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.magictower.primary.R;
import com.magictower.primary.engine.ContactInfoParser;
import com.magictower.primary.utils.SMSUitls;
import com.magictower.primary.utils.ToastUtils;

/**
 * @author kejunzhan
 * @since 2017/12/13
 */

public class AToolsActivity extends AppCompatActivity {
    private ProgressDialog pd;
    @ViewInject(R.id.progressBar)
    private ProgressBar progressBar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atools);
        ViewUtils.inject(this);
    }

    public void numberAddressQuery(View view) {
        startActivity(new Intent(AToolsActivity.this, AddressActivity.class));
    }

    public void appLock(View view) {
        startActivity(new Intent(AToolsActivity.this, AppLockActivity.class));
    }

    public void exportContact(View view) {
        pd = new ProgressDialog(AToolsActivity.this);
        pd.setTitle("提示");
        pd.setMessage("正在备份联系人，请等待...");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.show();
        new Thread(){
            @Override
            public void run() {
                boolean res = ContactInfoParser.backup(AToolsActivity.this, new ContactInfoParser.BackCall() {
                    @Override
                    public void before(int count) {
                        pd.setMax(count);
                    }

                    @Override
                    public void onBackUp(int progress) {
                        pd.setProgress(progress);
                    }
                });
                if (res) {
                    ToastUtils.showToast(AToolsActivity.this, "备份成功");
                } else {
                    ToastUtils.showToast(AToolsActivity.this, "备份失败");
                }
                pd.dismiss();
            }
        }.start();
    }

    public void backupSms(View view){
        pd = new ProgressDialog(AToolsActivity.this);
        pd.setTitle("提示");
        pd.setMessage("正在备份短信，请等待...");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.show();
        new Thread(){
            @Override
            public void run() {
                boolean res = SMSUitls.backup(AToolsActivity.this, new SMSUitls.BackUpCallBackSms() {
                    @Override
                    public void before(int count) {
                        progressBar.setMax(count);
                        pd.setMax(count);
                    }

                    @Override
                    public void onBackUpSms(int process) {
                        pd.setProgress(process);
                        progressBar.setProgress(process);
                    }
                });
                if (res) {
                    ToastUtils.showToast(AToolsActivity.this, "备份成功");
                } else {
                    ToastUtils.showToast(AToolsActivity.this, "备份失败");
                }
                pd.dismiss();
                progressBar.setProgress(0);//重置进度条
            }
        }.start();
    }
}
